package com.starblink.search.category.ui.newlist.cell

import android.content.Context
import android.graphics.Color
import androidx.recyclerview.widget.GridLayoutManager
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.apollo.id2String
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.util.FontUtil
import com.starblink.basic.ext.load
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.rocketreserver.fragment.CategoryD
import com.starblink.search.R
import com.starblink.search.category.data.CategoryModel
import com.starblink.search.category.data.CategoryWithParentId
import com.starblink.search.databinding.ItemMainCategoryBinding
import com.starblink.search.databinding.ItemThreeCategoryBinding
import com.starblink.search.databinding.ItemTwoCategoryBinding
import okhttp3.internal.notifyAll


class LeveMainCell(
    binding: ItemMainCategoryBinding,
    private val onClick: ((model: CategoryModel, position: Int) -> Unit)? = null,
) :
    BaseBindingVH<CategoryModel, ItemMainCategoryBinding>(binding) {

    override fun onBind(model: CategoryModel, position: Int) {
        binding.tvName.text = model.source.name

        binding.vLeft.visibleOrGone(model.expand) {
        }
        if (model.expand) {
            FontUtil.setMontseratBold(binding.tvName)
//            binding.content.setBackgroundResource(R.color.sk_gray0)
        } else {
            FontUtil.setMontseratMedium(binding.tvName)
//            binding.content.setBackgroundResource(R.color.sk_gray100)
        }
        if (model.source.id == "0") {
            binding.tvName.text = ""
        }
        binding.ivSaleZone.visibleOrGone(model.source.id == "0")

        itemView.setOnClickListener {
            if (!model.expand) {
                onClick?.invoke(model, position)
            }
        }
    }

}

class NewLevelTwoCell(
    val mContext: Context,
    binding: ItemTwoCategoryBinding,
    private val clickLL: (position: Int, snapshot: Boolean) -> Unit
) :
    BaseBindingVH<CategoryWithParentId, ItemTwoCategoryBinding>(binding) {

    override fun onBind(data: CategoryWithParentId, position: Int) {
        val model = data.category
        if (data.pCategoryId == "0") {//第一条是Sale Zone时
            binding.tvName.visibleOrGone(false)
            binding.llName.visibleOrGone(true) {
                binding.tvName2.text = model.name
                binding.tvDiscountLay.visibleOrGone(!model.highestDiscountInfo.isNullOrBlank()) {
                    binding.tvDiscount.text = "${model.highestDiscountInfo}"
                }

                if (data.expand) {
                    binding.cateArrow.setImageResource(R.drawable.ic_category_arr_top)
                    binding.tvDiscountLay.gone()
                } else {
                    binding.cateArrow.setImageResource(R.drawable.ic_category_arr_btm)
                    binding.tvDiscountLay.visible()
                }

                click {
                    clickLL.invoke(position, data.expand)
                }
            }
        } else {
            binding.tvName.visibleOrGone(true)
            binding.llName.visibleOrGone(false)
            binding.tvName.text = model.name
        }


        if (model.children != null && model.children!!.isNotEmpty()) {
            val list: List<CategoryD.Child> = model.children as List<CategoryD.Child>
            if (data.pCategoryId == "0") {
                if (data.expand) {
                    binding.recycler.visibleOrGone(true)
                } else {
                    binding.recycler.visibleOrGone(false)
                    return
                }
            } else {
                binding.recycler.visibleOrGone(true)
            }
            binding.recycler.layoutManager =
                GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false)
            if (binding.recycler.itemDecorationCount == 0) {
                binding.recycler.addItemDecoration(
                    SimpleGapItemDecor(horGap = 0, verGap = 12.dp2px(), withEdge = false)
                )
            }
            val productAdapter: MutableAdapter by lazyOnNone {
                MutableAdapter(list).apply {
                    addVhDelegate {
                        LeveThreeCell(
                            ItemThreeCategoryBinding.inflate(it.inflater, it, false),
                            data.pCategoryId == "0"
                        )
                    }
                }
            }
            binding.recycler.adapter = productAdapter
        } else {
            binding.recycler.visibleOrGone(false)
        }
    }

}

class LeveThreeCell(binding: ItemThreeCategoryBinding, private val isSaleZone: Boolean) :
    BaseBindingVH<CategoryD.Child?, ItemThreeCategoryBinding>(binding) {

    override fun onBind(model: CategoryD.Child?, position: Int) {
        if (model != null) {
            binding.tvName.text = model.categoryF.name
            if (model.categoryF.imageUrl?.isNotEmpty() == true) {
//                binding.ivLogo.load(model.categoryF.littleImageUrl!!)
                binding.ivLogo.load(model.categoryF.imageUrl!!, 60.dp, 60.dp)
            }
            binding.tvDiscount.visibleOrGone(!model.categoryF.highestDiscountInfo.isNullOrEmpty()) {
                text = "Up to ${model.categoryF.highestDiscountInfo}"
            }

            itemView.setOnClickListener {
                itemView.context.navigationTo(RoutePage.Search.PAGE_CATEGORY_GOODS) {
                    extraOf(
                        RoutePage.ARG.ID to (model.categoryF.id2String),
                        RoutePage.ARG.TITLE to (model.categoryF.name),
                        RoutePage.ARG.IS_SALE_ZONE to isSaleZone
                    )
//                    extraOf(RoutePage.ARG.TITLE to (model.categoryF.name))
//                    extraOf(RoutePage.ARG.IS_SALE_ZONE to isSaleZone)
                }
            }
        }
    }

}
